<template>
  <div class="dialog">
    <el-dialog :title="'摄像头' + titleText" :visible.sync="showDialog" width="30%" :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="120px" class="demo-ruleForm">
        <el-form-item v-if="nowChanceStau === 'add'" label="物联摄像头选择" prop="iotCameraId">
          <el-select class="w100p" v-model="formData.iotCameraId" filterable clearable @change="cameraChange">
            <el-option v-for="item of cameraList" :key="item.cameraId + 'cameraId'" :label="item.cameraName"
              :value="item.cameraId" />
          </el-select>
        </el-form-item>
        <el-form-item label="摄像头序列号" v-if="nowChanceStau === 'edit'">
          <el-input disabled v-model="formData.deviceSerial" clearable  />
        </el-form-item>
        <el-form-item label="自定义名称" prop="cameraName">
          <el-input v-model="formData.cameraName" clearable placeholder="请输入自定义摄像头名称" />
        </el-form-item>
        <!-- <el-form-item  label="鱼塘:" class="w50p" prop="fishPondId">
          <el-select
            v-model="formData.fishPondId"
            filterable
            clearable
            placeholder="请选择鱼塘"
            @change="queryDevice"
          >
            <el-option
              v-for="item of pondList"
              :key="item.fishPondId + 'fishPondId'"
              :label="item.pondName"
              :value="item.fishPondId"
            />
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="设备:" class="w50p" prop="equipmentId">
          <el-select
            v-model="formData.equipmentId"
            filterable
            clearable
            multiple
            placeholder="请选择设备"
          >
            <el-option
              v-for="item of deviceList"
              :key="item.equipmentId + 'equipmentId'"
              :label="item.equipmentName"
              :value="item.equipmentId"
            />
          </el-select>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import AreaApi from "@/api/basic-info-manage/area"
import PondApi from "@/api/basic-info-manage/fishPond"
import DeviceApi from "@/api/basic-info-manage/device-management"
import CameraApi from "@/api/iot-monitoring/camera-management"

export default {
  name: '',
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      formData: {},
      cameraList: [],
      areaList: [],
      pondList: [],
      deviceList: [],
      rules: {
        iotCameraId: [
          { required: true, message: "请选择物联摄像头", trigger: ["change", "blur"] }
        ],
        cameraName: [
          { required: true, message: "请输入自定义摄像头名称", trigger: ["change", "blur"] }
        ],
        // fishPondId: [
        //   { required: true, message: "请选择鱼塘", trigger: ["change", "blur"] }
        // ],
        equipmentId: [
          { required: true, message: "请选择设备", trigger: ["change", "blur"] }
        ],
      },
      showDialog: this.show
    };
  },
  computed: {
  },
  watch: {
    show(v) {
      this.showDialog = v;
    },
    nowChanceStau(v) {
      if (v === 'add') {
        this.formData = {
          // staffName: sessionStorage.getItem('staffName'),
        }
        this.$nextTick(() => {
          this.$refs['ruleForm'].clearValidate()
        })
      }
      if (v === 'edit') {
        this.formData = this.editObject
      }
    }
  },
  created() {
  },
  mounted() {
    this.queryCameraByMark()
    this.queryDevice()
    // this.queryArea()
    this.queryPond()
  },
  methods: {
    queryArea() {
      AreaApi.queryByCondition().then(res => {
        this.areaList = res.data
      })
    },
    queryPond() {
      PondApi.queryByCondition().then(res => {
        this.pondList = res.data
      })
    },
    queryDevice(e) {
      DeviceApi.queryByCondition({ fishPondId: e }).then(res => {
        this.deviceList = res.data
      })
    },
    queryCameraByMark() {
      CameraApi.queryByCompanyMark().then(res => {
        this.cameraList = res.data.cameraList
      })
    },
    cameraChange(e) {
      this.cameraList.find(item => {
        if (item.cameraId === e) {
          this.$set(this.formData, 'iotCameraId', item.cameraId)
          // this.$set(this.formData,'cameraId',item.cameraId)
          this.$set(this.formData, 'cameraName', item.cameraName)
          this.$set(this.formData, 'channelNo', item.channelNo)
          this.$set(this.formData, 'controlFlag', item.controlFlag)
          this.$set(this.formData, 'deviceSerial', item.deviceSerial)
          this.$set(this.formData, 'ownershipId', item.ownershipId)
          this.$set(this.formData, 'screenshot', item.screenshot)
          this.$set(this.formData, 'solarEnergyCamera', item.solarEnergyCamera)
          this.$set(this.formData, 'verificationCode', item.verificationCode)
          return true
        }
      });
    },
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            CameraApi.edit(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          } else if (this.nowChanceStau === "add") {
            CameraApi.add(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("添加成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped></style>
